package com.fitnessapp.main;

import java.util.List;
import java.util.Map;

import com.fitnessapp.database.AdapterFromQueryThread;
import com.fitnessapp.database.MySqlHelper;
import com.fitnessapp.database.DatabaseStrings;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;

import android.widget.SimpleAdapter;


public class CustomListView extends ListActivity {	
	
	
	private String muscleGroup;
	private ProgressDialog progress;

	private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            setListAdapter((ListAdapter)msg.obj);
        }
	};

	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
	//	setContentView(R.layout.full_screen_list_view);
		
		muscleGroup = getIntent().getStringExtra("muscle");

		progress = ProgressDialog.show(this, "", "Searching...", true);	
		
		getListView().setOnItemClickListener(new ExerciseClickListener());
		
		(new AdapterFromQueryThread(this, mHandler, progress,
				"exercise_id, exercise_name",
				DatabaseStrings.EXERCISE_TABLE,
				"exercise_muscle = \'" + muscleGroup +"\'")).start();	
	}
	
	
	private class ExerciseClickListener implements OnItemClickListener{

		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			
			Intent intent = new Intent(CustomListView.this, ExerciseDisplay.class);
			Map<String,String> exerciseData = (Map<String,String>)parent.getAdapter().getItem(position);
			intent.putExtra(ExerciseDisplay.EXERCISE_PRIMARY_KEY, exerciseData.get(ExerciseDisplay.EXERCISE_PRIMARY_KEY));
			startActivity(intent);
		}
		
		
	}
	
	


}
